package com.bw.zcb.mvp.base.presenter;

import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.model.IModel;
import com.bw.zcb.mvp.base.view.IView;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BasePresenter<M extends IModel,V extends IView> implements IPresenter, Observer<BaseEntity> {

    private CompositeDisposable compositeDisposable;//RXJava下专门用来保存
    protected M m;
    protected V v;

    public BasePresenter( M m, V v) {
        this.m = m;
        this.v = v;
        compositeDisposable = new CompositeDisposable();

    }
    //RXJava在订阅成功后的disposable参数的,当异步任务未执行完成,且界面被用户手动关闭
    //在通过lifecycle关联的声明周期方法onDestroy中接触rxjava异步订阅任务防止内存泄漏

    @Override
    public void Destroy() {
        //解除全部异步订阅任务
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
        v.showDialog();
    }


    @Override
    public void onError(Throwable e) {
        v.showMsg(e.getMessage());
    }

    @Override
    public void onComplete() {
        v.hideDialog();
    }
}
